package com.departmentstore.dto;

import com.departmentstore.entity.DepartmentProduct;
import com.departmentstore.entity.Department;
import com.departmentstore.entity.Product;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Optional;

@Data
public class DepartmentProductDTO {
    private String departmentId;
    private String departmentName;  // 部门名称
    private String productId;
    private String productName;     // 商品名称
    private BigDecimal price;       // 商品价格
    private Integer quantity;
    private String description;
    private String model;

    // 构造函数（Lombok 不会为带参构造生成默认构造，需显式定义无参构造）
    public DepartmentProductDTO() {}

    public DepartmentProductDTO(DepartmentProduct dp) {
        this.departmentId = dp.getDepartmentId();
        this.departmentName = Optional.ofNullable(dp.getDepartment())
                .map(Department::getName)
                .orElse("未知部门");  // 处理空值
        this.productName = Optional.ofNullable(dp.getProduct())
                .map(Product::getName)
                .orElse("未知商品");
        this.departmentName = dp.getDepartment().getName();
        this.productId = dp.getProductId();
        this.productName = dp.getProduct().getName();
        this.price = dp.getProduct().getPrice();
        this.quantity = dp.getQuantity();
        this.description = dp.getProduct().getDescription();
        this.model = dp.getProduct().getModel();
    }
}
